<?
include "include/config.php";

$_GET['v']=addslashes($_GET['v']);
$_GET['i']=addslashes($_GET['i']); 
$_GET['ap']=addslashes($_GET['ap']);
	
$tip=array ('1'=>'Garsoniera','2'=>'Apartament 2 camere','3'=>'Apartament 3 camere','4'=>'Apartament 4 camere','5'=>'Teren','6'=>'Spatiu Comercial','7'=>'Casa/Vila');
$oferta=array ('1'=>'Vanzare','2'=>'Inchiriere');
$smarty->assign('tip',$tip);
$smarty->assign('oferta',$oferta);
	
     $valuta=array ('0'=>'Discutabil','1'=>'RON','2'=>'EURO','3'=>'USD'); 
 $smarty->assign('valuta',$valuta); 
		

	SmartyPaginate::connect();
	if ($_GET['r']!='')
	SmartyPaginate::reset();
    // set items per page
    SmartyPaginate::setLimit(10);

	if ($_GET['v']!='') $oferta_query=" AND oferta=1 ";
	if ($_GET['i']!='') $oferta_query=" AND oferta=2 ";	
	
	if ($_GET['ap']==1) $tip_query=" AND (tip=1 or tip=2 or tip=3 or tip=4)";
	if ($_GET['ap']==2) $tip_query=" AND tip=7";
	if ($_GET['ap']==3) $tip_query=" AND tip=6";
	if ($_GET['ap']==4) $tip_query=" AND tip=5";
	
	
	if ($_GET['ap']=='') $oferta_query=" AND tip=1";
	

	if (($_GET['ap']!='')AND($_GET['v']!=''))
	SmartyPaginate::setUrl("list.php?ap=".$_GET['ap']."&v=".$_GET['i']);
	if (($_GET['ap']!='')AND($_GET['i']!=''))
	SmartyPaginate::setUrl("list.php?ap=".$_GET['ap']."&v=".$_GET['v']);
	
    if ((($_GET['ap']=='')AND($_GET['v']==''))OR(($_GET['ap']=='')AND($_GET['i']=='')))
	SmartyPaginate::setUrl("list.php?ap=1&v=1");
	
    $clauza=$tip_query.$oferta_query;
	
	// assign your db results to the template
    $smarty->assign('oferte', get_db_results());
    
	// assign {$paginate} var
	SmartyPaginate::assign($smarty);
    
	function get_db_results() {
	    global $clauza;
	
   $_query = sprintf("select SQL_CALC_FOUND_ROWS oferta.*,locatii.name,parent.name as parent from oferta left join locatii on oferta.id_loc=locatii.id left join locatii as parent on parent.id=locatii.id_parent where spof=0 ".$clauza." ORDER BY oferta.id desc LIMIT %d,%d", SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());
       $_result = mysql_query($_query);
	   //echo  $_query ;
	   list($t_query)=mysql_fetch_array (mysql_query("select FOUND_ROWS()"));
	   while ($cereri_array=mysql_fetch_array($_result))   
       {    
       list($idp,$numep)=mysql_fetch_array(mysql_query("select L0.id,L0.name from locatii L0 left join locatii L1 on L1.id_parent=L0.id where L1.id='".$cereri_array['parentid']."'   ")); 
               
       if ($idp!='') $cereri_array['parent']=$numep;
	   $_data[]=$cereri_array;   
       }
			
 		$_result = mysql_query($_query);
		SmartyPaginate::setTotal($t_query);
        mysql_free_result($_result);
        return $_data;
    } 

$smarty->assign("content", $smarty->fetch(tmpd.'list'.tmpl_ext));
include "include/make_page.php";
?>